package com.android.mybaidu;

import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;

public class Main extends MapActivity {

	// 添加百度地图的相关控件
	private MapView mapView;
	private BMapManager bMapManager;// 加载地图的引擎
	// 百度地图的key
	private String keyString = "A270F85CD72A01E8519A9677A75FB4016ED9A5A3";
	// 在百度地图上添加一些控件，比如是放大或者缩小的控件
	private MapController mapController;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapView = (MapView) this.findViewById(R.id.bmapView);
		bMapManager = new BMapManager(Main.this);
		// 必须要加载key
		bMapManager.init(keyString, new MKGeneralListener() {

			@Override
			public void onGetPermissionState(int arg0) {
				if (arg0 == 300) {
					Toast.makeText(Main.this, "输入的Key有错！请核实！！", 1).show();
				}
			}

			@Override
			public void onGetNetworkState(int arg0) {

			}
		});

		this.initMapActivity(bMapManager);
		mapView.setBuiltInZoomControls(true);// 表示可以设置缩放功能
		mapController = mapView.getController();
		// 需要定义一个经纬度：北京天安门
		GeoPoint geoPoint = new GeoPoint((int) (39.915 * 1E6),
				(int) (116.404 * 1E6));

		mapController.setCenter(geoPoint);// 设置一个中心点
		mapController.setZoom(12);// 设置缩放级别是12个级别

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (bMapManager != null) {
			bMapManager.destroy();
			bMapManager = null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (bMapManager != null) {
			bMapManager.start();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (bMapManager != null) {
			bMapManager.stop();
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}